package main


import (
  "xggorm"
  "fmt"
  "gorm.io/gorm"
  _ "gitee.com/XuguDB/go-xugu-driver"
)


type User struct {
    ID   int   `gorm:"primaryKey"`
    Name string `gorm:"size:255"`
    Age  int
}


func main() {
    dsn := "IP=192.168.2.216;DB=SYSTEM;User=SYSDBA;PWD=SYSDBA;Port=5138;CURRENT_SCHEMA=SYSTEM;AUTO_COMMIT=on;CHAR_SET=UTF8"
		
    db, err := gorm.Open(xggorm.Open(dsn), &gorm.Config{})
    if err != nil {
	fmt.Printf("gorm open fail, err:%v dsn:%v\n", err, dsn)
    }

    if db != nil{
	fmt.Println("open conn sucess")
    }
// 自动迁移表结构
    err = db.AutoMigrate(&User{})
    if err != nil {
        panic("failed to migrate table")
    }

    // 插入数据
    user := User{Name: "Jack", Age: 18}
    result := db.Create(&user)
    if result.Error != nil {
        panic("failed to create user")
    }

    // 查询用户
    var users []User
    result = db.Find(&users)
    if result.Error != nil {
        panic("failed to query users")
    }
    for _, user := range users {
        println(user.ID, user.Name, user.Age)
    }

    // 更新用户
    user.Name = "Tom"
    user.Age = 20
    result = db.Save(&user)
    if result.Error != nil {
        panic("failed to update user")
    }

    db.First(&user,users[0].ID)
    // 删除用户
    result = db.Delete(&user)
    if result.Error != nil {
        panic("failed to delete user")
    }

}
